package ink.lovejinhu.swardToOffer;

import ink.lovejinhu.common.ListNode;

/**
 * @author jinhu
 * created 2021-08-18 9:28
 */
public class Offer_022 {
    /**
     * 链表中的倒数第K个节点
     *
     * @param head
     * @param k
     * @return
     */
    public ListNode getKthFromEnd(ListNode head, int k) {
        /**
         * 搞一个K长度的双指针
         */
        ListNode k_head = head;
        ListNode pre = new ListNode();
        pre.next = head;
        for (int i = 1; i < k; i++) {
            k_head = k_head.next;
        }
        while (k_head != null) {
            k_head = k_head.next;
            pre = head;
            head = head.next;
        }
        return pre;
    }
}
